﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Text.RegularExpressions;

namespace System.Web.Mvc.Html
{
    public static class BuckeyeExtensions
    {
        public static MvcHtmlString CheckPermission(this HtmlHelper helper, int RoleID, int ModuleID, string Ope)
        {
            StringBuilder sb = new StringBuilder();
            Manager.BLL.Admin.M_RoleModuleBll bllRM = new Manager.BLL.Admin.M_RoleModuleBll();
            System.Data.DataSet ds = bllRM.GetList("RoleID=" + RoleID + " and ModuleID=" + ModuleID);
            System.Data.DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                string cks = dt.Rows[0]["Permission"].ToString();
                if (cks.IndexOf(Ope) > -1)
                {
                    sb.Append("checked=\"checked\"");
                }
                else
                    sb.Append("");
            }
            else
                sb.Append("");

            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 格式化字符串
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="source"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static MvcHtmlString FormatStr(this HtmlHelper helper, string source,int length)
        {
            if (source != null)
            {
                if (source.Length >= length)
                    return new MvcHtmlString(source.Substring(0,length) + "...");
                else
                    return new MvcHtmlString(source);
            }
            else
                return new MvcHtmlString("...");
        }

        public static MvcHtmlString FormatHtmlStr(this HtmlHelper helper, string Htmlstring, int length)
        {
            if (Htmlstring != null)
            {
                Htmlstring = Htmlstring.Replace("<br/>", "\n");
                Htmlstring = Htmlstring.Replace("<br/>", "\r\n");
                Htmlstring = Htmlstring.Replace("&lt;", "<");
                Htmlstring = Htmlstring.Replace("&gt;", ">");
                Htmlstring = Htmlstring.Replace("&quot;", "\"");
                Htmlstring = Htmlstring.Replace("&nbsp;", " ");
                Htmlstring = Htmlstring.Replace("&amp;", "");
                Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);

                Htmlstring.Replace("<", "");
                Htmlstring.Replace(">", "");
                Htmlstring.Replace("\r\n", "");

                //对字符串长度进行判断。然后依据长短进行截取
                if (Htmlstring.Length < length)
                {//如果长度小于40个汉字
                    return new MvcHtmlString(Htmlstring);
                }
                else
                {
                    Htmlstring = Htmlstring.Substring(0, length);
                    return new MvcHtmlString(Htmlstring + "...");
                }
            }
            else
            {
                return new MvcHtmlString("...");
            }
        }
    }
}